package cn.hh.pet.controller;

import cn.hh.basic.util.AjaxResult;
import cn.hh.basic.util.LoginContext;
import cn.hh.basic.util.PageList;
import cn.hh.pet.domain.Pet;
import cn.hh.pet.domain.SMM;
import cn.hh.pet.query.SMMQuery;
import cn.hh.pet.service.ISMMService;
import cn.hh.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/SMM")
public class SMMController {
    @Autowired
    private ISMMService smmService;

    /*发布寻主信息*/
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SMM smm, HttpServletRequest req){
        try {
            /*获取登录信息*/
            LoginInfo logininfo = LoginContext.getLogininfo(req);
            /*传入发布信息和登录信息*/
            smmService.publish(smm,logininfo.getId());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("发布失败");
        }
    }
    /*待处理消息，平台管理员查所有，店铺管理员及员工只能查自己店铺的*/
    @PostMapping("/pending")
    public PageList<SMM> pending(@RequestBody SMMQuery query,HttpServletRequest req){
        try {
            LoginInfo logininfo = LoginContext.getLogininfo(req);
            return  smmService.pending(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /*已处理消息，平台管理员查所有，店铺管理员及员工只能查自己店铺的*/
    @PostMapping("/processed")
    public PageList<SMM> processed(@RequestBody SMMQuery query,HttpServletRequest req){
        try {
            LoginInfo logininfo = LoginContext.getLogininfo(req);
            return  smmService.processed(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /*前台用户查看自己的消息，只能查看自己发布的*/
    @PostMapping("/list")
    public PageList<SMM> list(@RequestBody SMMQuery query,HttpServletRequest req){
        try {
            LoginInfo logininfo = LoginContext.getLogininfo(req);
            return  smmService.list(query,logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /*处理宠物*/
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet,HttpServletRequest req){
        try {
            LoginInfo logininfo = LoginContext.getLogininfo(req);
            smmService.handle(pet,logininfo.getId());
            return  AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("处理失败"+e.getMessage());
        }
    }
}
